<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:金币管理
// +----------------------------------------------------------------------


namespace app\admin\controller;
use app\BackendController;
use app\admin\model\UserMoneyLog as cmodel;
 
use think\facade\Cache;
use support\Request;
use think\facade\Db;

class MoneyLogController extends BackendController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        //过帐类型
        $tmp             = cmodel::getOpType();
        $op_type_options = output_select($tmp, '');

        return view('moneylog/index', compact('op_type_options'));
    }

    /**
     * 列表json数据
     * @param  Request $request [请求变量]
     * @return [type]           [json数据]
     */
    public function listData(Request $request)
    {
        $limit = $request->get('limit', "10");
        $sort  = $request->get("sort", 'id');
        $order = $request->get("order", "DESC");
        $w     = [];

        if ($request->get('memo')) {
            $w[] = ['a.memo', 'like', '%' . $request->get('memo') . '%'];

        }

        if ($request->get('u_id')) {
            $w[] = ['a.u_id', '=', $request->get('u_id')];

        }

        if ($request->get('op_type')) {
            $w[] = ['a.op_type', '=', $request->get('op_type')];

        }

        $q = cmodel::alias('a')->where($w);
        $q->order($sort, $order);

        $ls      = $q->paginate(['list_rows' => $limit, 'request' => $request->all()]);
        $res     = $ls->items();
        $op_type = cmodel::getOpType();
        $res     = array_map(function ($v) use ($op_type) {
            $v->money       = $v->plus - $v->reduce;
            $v->op_type_txt = $op_type[$v->op_type];
            $v->user_info   = $v->user_info;
            $v->profile     = $v->user_info->profile;
            return $v;
        }, $res);

        return json(['code' => $ls->total() > 0 ? 0 : 201, 'msg' => '无数据', 'count' => $ls->total(), 'data' => $res]);
    }

    /**
     * 增加
     * @return [type] [description]
     */
    public function create()
    {
        return view('moneylog/create');
    }

    /**
     * 保存增加平账
     * @return [type] [description]
     */
    public function save(Request $request)
    {
        Db::startTrans();
        try {
            $cmodel                             = new cmodel;
            $post                               = $request->post();
            $post['op_type']                    = 6;
            $post['op_key']                     = $cmodel::max("id") + 1;
            $post['plus']                       = $post['reduce']                       = 0;
            $post['mtype'] == 1 ? $post['plus'] = $post['amount'] : $post['reduce'] = $post['amount'];
            $valid                              = new \app\admin\validate\MoneyLog;
            $r                                  = $valid->check($post);
            if (!$r) {
                throw new \Exception($valid->getError(), 1);

            }
            if ($r) {
                $before         = \app\admin\model\User::where('u_id', $post['u_id'])->lock(true)->value('money');
                $post['before'] = $before;
                $post['after']  = $before + $post['plus'] - $post['reduce'];
                $cmodel->save($post);
                Db::name("user")->where('u_id', $post['u_id'])->inc('money', $post['plus'] - $post['reduce'])->update();
                $msg = ['status' => 1, 'msg' => '保存成功'];
                Db::commit();
            }
        } catch (\Exception $e) {
            $msg = ['status' => 0, 'msg' => $e->getMessage()];
        }
        return json($msg);

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request)
    {
        $id = $request->get("id");
        $data = cmodel::find($id);

        return view('moneylog/edit', compact('data'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $id = $request->get("id");
        $post  = $request->post();
        $valid = new \app\admin\validate\MoneyLog;
        $r     = $valid->scene('edit')->check($post);

        $cmodel = new cmodel;
        $info   = $cmodel->where('id', $id)->find();
        if (!$r) {
            $msg = ['status' => -1, 'msg' => $valid->getError()];
        } else {

            $info->allowField(['memo'])->save($post);

            $msg = ['status' => 1, 'msg' => '保存成功'];
        }
        return json($msg);

    }

}
